package io.xxx.omni.oss.custom

import com.google.gson.JsonObject
import io.xxx.omni.oss.common.format
import io.xxx.omni.oss.common.toJsonString
import io.xxx.omni.oss.common.toLocalDateTime
import io.xxx.omni.oss.domain.Document
import io.xxx.omni.oss.domain.DocumentType

@Suppress("unused")
class DdTradeSynchronizer : DdSynchronizer() {

    override val documentType = DocumentType.TRADE

    override val method = "dangdang.orders.list.get"

    override val v = 3.0
    override fun getCount(context: Context, response: JsonObject): Int {
        return response["totalInfo"].asJsonObject["orderCount"].asInt
    }

    override fun buildDocuments(context: Context, response: JsonObject): List<Document> {
        return response.getAsJsonArray("OrdersList")
            .map {
                it as JsonObject
                Document(
                    it["orderID"].asString,
                    it["orderState"].asString,
                    it.toJsonString(),
                    it["orderTimeStart"].toLocalDateTime(),
                    it["lastModifyTime"].toLocalDateTime(),
                )
            }
    }

    override fun buildRequest(context: Context, pageNo: Int): Map<String, Any> {
        val (startTime, endTime) = context
        return mapOf(
            "os" to "9999",
            "lastModifyTime_start" to startTime.format(),
            "lastModifyTime_end" to endTime.format(),
            "p" to pageNo,
            "pageSize" to pageSize,
        )
    }
}